package com.topscore.uke.ui.fragments;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadService;
import com.kingja.loadsir.core.LoadSir;
import com.topscore.uke.R;
import com.topscore.uke.entities.CouponInfo;
import com.topscore.uke.mvp.contractors.GetCouponsInfoContractor;
import com.topscore.uke.mvp.presenters.GetCouponsInfoPresenter;
import com.topscore.uke.ui.BaseSupportFragment;
import com.topscore.uke.ui.loadsir_callback.ErrorCallBack;
import com.topscore.uke.ui.loadsir_callback.LoadingCallBack;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class CouponInOccupiedFragment extends BaseSupportFragment implements GetCouponsInfoContractor.View {

    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;

    private View rootView;

    private RecyclerAdapter mAdapter;

    private GetCouponsInfoContractor.Presenter mCouponPresenter;

    private LoadService mLoadService;

    public CouponInOccupiedFragment() {
        // Required empty public constructor
        mCouponPresenter = new GetCouponsInfoPresenter(this);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        rootView = inflater.inflate(R.layout.fragment_coupons, container, false);
        ButterKnife.bind(this, rootView);
        initViews();
        return rootView;
    }

    private void initViews() {
        mLoadService = LoadSir.getDefault().register(recyclerView, new Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {

            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        mAdapter = new RecyclerAdapter();
        recyclerView.setAdapter(mAdapter);
        initData();
    }

    private void initData() {
        mCouponPresenter.getCoupons("occupy");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onLoading(String loadingMessage) {
        mLoadService.showCallback(LoadingCallBack.class);
    }

    @Override
    public void onLoadFinished() {
        mLoadService.showSuccess();
    }

    @Override
    public void onFail(int code, String errorMessage) {
        mLoadService.showCallback(ErrorCallBack.class);
    }

    @Override
    public void onGetCouponDataSuccess(List<CouponInfo> data) {
        mAdapter.setNewData(data);
    }

    private class RecyclerAdapter extends BaseQuickAdapter<CouponInfo, BaseViewHolder> {
        View emptyView;

        public RecyclerAdapter() {
            super(R.layout.coupon_in_occupied_item, new ArrayList<>());

            emptyView = LayoutInflater.from(getActivity()).inflate(R.layout.coupon_empty_layout, (ViewGroup) recyclerView.getParent(), false);
            TextView textView = (TextView) emptyView.findViewById(R.id.empty_message);
            textView.setText("暂无已占用的卡券");
            setEmptyView(emptyView);
        }

        @Override
        protected void convert(BaseViewHolder holder, CouponInfo item) {
            holder.setText(R.id.time_tv, item.getStartTime() + "\t" + "至" + "\t" + item.getEndTime())
                    .setText(R.id.type_tv, item.getCouponTypeName())
                    .setText(R.id.brand_iv, item.getBrandName())
                    .setText(R.id.discount_amount_tv, "￥" + item.getDiscountAmt());
        }
    }

}
